function Game(){
    this.i18n = {
        get: function(obj){
            return Language[obj] ? Language[obj] : "?";
        }
    }
}

var game = new Game();

Game.prototype.backToMainMenu = function(){
    var scene = cc.Scene.create();
    var layer = new MainMenuLayer();
    layer._anim = "Backin";
    if (layer && layer.init()) {
        scene.addChild(layer);
        cc.Director.getInstance().replaceScene(scene);
    }
}

Game.prototype.switchSoundOnOff = function(){
    sound.switchOnOff();
}

Game.prototype.updateSoundButton = function(btn){
    if (sound.isSoundOn()) {
        btn.setNormalImage(cc.Sprite.create(s_MusicButton));
        btn.setSelectedImage(cc.Sprite.create(s_MusicButtonPressed));
        btn.setDisabledImage(cc.Sprite.create(s_MusicButton));
    } else {
        btn.setNormalImage(cc.Sprite.create(s_MusicButtonOff));
        btn.setSelectedImage(cc.Sprite.create(s_MusicOffButtonPressed));
        btn.setDisabledImage(cc.Sprite.create(s_MusicButtonOff));
    }
}

Game.prototype.switchLanguage = function(){
    var cur = config.getCurrentLanguageIndex();
    cur++;
    if (cur > config.kLanguages.length - 1) {
        cur = 0;
    }
    config.setCurrentLanguage(cur);
    util.i18n.callback = function(){
        var m = util.i18n.members;
        for (i in m) {
            util.replaceCCBText(m[i][0], m[i][1]);
        }
    };
    util.i18n.setLanguage(config.getCurrentLanguage());
}

Game.prototype.updateLanguageButton = function(btn){
    var cur = config.getCurrentLanguageIndex();
    btn.setNormalImage(cc.Sprite.create(language_button(cur)))
    btn.setSelectedImage(cc.Sprite.create(language_button(cur, true)))
    btn.setDisabledImage(cc.Sprite.create(language_button(cur)))
}

Game.prototype.enterSelectLevels = function(){
    var layer = new SelectLevelLayer();
    layer._curPlayLevelId = parseInt(storage.get("nextUnpassLevel"));
    layer._maxPlayableLevelId = parseInt(storage.get("unlockedLevel"));
    layer._maxLevelId = parseInt(storage.get("IAP_LEVEL"));
    
    if (layer && layer.init()) {
        var scene = cc.Scene.create();
        scene.addChild(layer);
        cc.Director.getInstance().replaceScene(scene);
    }
}

Game.prototype.playLevel = function(levelId){
    cc.log('Play level ' + levelId);
    
    var layer = new PlayLevelLayer();
    layer._level = levelId;
    if (layer && layer.init()) {
        var scene = cc.Scene.create();
        scene.addChild(layer);
        cc.Director.getInstance().replaceScene(scene);
    }
}

Game.prototype.completeLevel = function(levelId){
    var unlockedLevel = parseInt(storage.get('unlockedLevel'));
    var nextUnpassLevel = parseInt(storage.get('nextUnpassLevel'));
    var IAP_LEVEL = parseInt(storage.get('IAP_LEVEL'));
    
    if (levelId >= nextUnpassLevel) {
        nextUnpassLevel = levelId + 1;
    }
    if (nextUnpassLevel > IAP_LEVEL) {
        nextUnpassLevel = IAP_LEVEL;
    } else if (nextUnpassLevel > unlockedLevel) {
        nextUnpassLevel = unlockedLevel;
    }
    storage.set('nextUnpassLevel', nextUnpassLevel);
}

Game.prototype.getNextPlayLevel = function(levelId){
    var nextPlayLevel = parseInt(levelId) + 1;
    
    if (nextPlayLevel > parseInt(storage.get('IAP_LEVEL'))) {
        nextPlayLevel = -1;
    } else if (nextPlayLevel > parseInt(storage.get('unlockedLevel'))) {
        nextPlayLevel = 0;
    }
    cc.log("getNextPlayLevel  levelid=" + levelId + " unlockedLevel=" + storage.get('unlockedLevel') + "  nextPlayLevel=" + nextPlayLevel);
    return nextPlayLevel;
}

Game.prototype.getScore = function(){
    return parseInt(storage.get('score'));
}

Game.prototype.setScore = function(s){
    storage.set('score', s);
}
